package com.liugege.make.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GameProxy implements InvocationHandler {
     private  Object object = null;

    public GameProxy(Object object) {
        this.object = object;
    }

    public GameProxy() {
    }

    /**
     * @author: Liu XingZhi
     * @Date: 2023/5/18 11:48
     * @description(方法描述)   重新InvocationHandler接口，增强被代理实例function功能
     * @method(方法名)
     * @param
     * @return
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before:" + log(method.getName()));
        System.out.println("end:" + log(method.getName()));
        return method.invoke(object,args);
    }
    /**
     * @author: Liu XingZhi
     * @Date: 2023/5/18 11:47
     * @description(方法描述)   动态生成一个代理实例
     * @method(方法名)
     * @param
     * @return
     */
    Object getProxyInstance(){
        return java.lang.reflect.Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this);
    }

    private String log(String methodName){
        return "log:【" + getDate() + "】" +  methodName + "方法正在执行";
    }

    public static String getDate() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = dateFormat.format(date);
        return dateString;
    }
}
